package com.sakiko.audiobook.search.api;

import com.sakiko.audiobook.common.result.Result;
import com.sakiko.audiobook.model.search.AlbumInfoIndex;
import com.sakiko.audiobook.query.search.AlbumIndexQuery;
import com.sakiko.audiobook.search.service.SearchService;
import com.sakiko.audiobook.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.mockito.exceptions.misusing.RedundantListenerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"all"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    /**
     * 查看排行榜 (从缓存中检索)
     *
     * @param category1Id
     * @param rankingDimension
     * @return
     */
    @Operation(summary = "查看排行榜")
    @GetMapping("/findRankingList/{category1Id}/{rankingDimension}")
    public Result findRankingList(@PathVariable Long category1Id, @PathVariable String rankingDimension) {
        List<AlbumInfoIndex> albumInfoIndexList = searchService.findRankingList(category1Id, rankingDimension);
        return Result.ok(albumInfoIndexList);
    }

    /**
     * 获取最新专辑排行榜
     *
     * @return
     */
    @Operation(summary = "更新排行榜")
    @GetMapping("updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

    /**
     * 自动补全
     *
     * @param keyword
     * @return
     */
    @Operation(summary = "自动补全")
    @GetMapping("/completeSuggest/{keyword}")
    public Result completeSuggest(@PathVariable String keyword) {
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }

    /**
     * 根据一级分类Id获取频道页数据
     *
     * @param category1Id
     * @return
     */
    @Operation(summary = "根据一级分类Id获取频道页数据")
    @GetMapping("/channel/{category1Id}")
    public Result getChannel(@PathVariable Long category1Id) {
        // 调用服务层方法  list<三级分类名称:专辑列表>
        List<Map<String, Object>> mapList = searchService.getChannel(category1Id);
        // 返回数据
        return Result.ok(mapList);
    }

    /**
     * 检索
     *
     * @param albumIndexQuery
     * @return
     */
    @Operation(summary = "检索")
    @PostMapping
    public Result search(@RequestBody AlbumIndexQuery albumIndexQuery) { // 确认请求参数
        // 确认返回值
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }

    /**
     * 专辑上架
     *
     * @param albumId
     * @return
     */
    @Operation(summary = "专辑上架")
    @GetMapping("/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable Long albumId) {
        // 调用服务层方法
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    /**
     * 专辑下架
     *
     * @param albumId
     * @return
     */
    @Operation(summary = "专辑下架")
    @GetMapping("/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable Long albumId) {
        // 调用服务层方法
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    /**
     * 批量上架 (模拟，也可改为多线程) 场景:
     * 后台存在全选按钮: List<Long> albumIdList
     * 文件上传: 读取 Excel中的 albumId
     *
     * @return
     */
    @Operation(summary = "批量上架")
    @GetMapping("batchUpperAlbum")
    public Result batchUpperAlbum() {
        //  循环
        for (long i = 1; i <= 1500; i++) {
            searchService.upperAlbum(i);
            // 失败策略: catch -> 记录 id -> continue
        }
        //  返回数据
        return Result.ok();
    }
}

